package com.fileLoader.dao.impl;


import java.util.List;

import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import com.fileLoader.dao.mongoDao;
import com.fileLoader.util.PagesInfo;

public class mongoDaoImpl implements mongoDao {
	
	public MongoTemplate mongoTemplate;

	public MongoTemplate getMongoTemplate() {
		return mongoTemplate;
	}

	public void setMongoTemplate(MongoTemplate mongoTemplate) {
		this.mongoTemplate = mongoTemplate;
	}

	public Object saveObject(Object object) {
		mongoTemplate.save(object);
		return object;
	}

	public void removeObject(Object object) {
		mongoTemplate.remove(object);
	}
	public void removeObjectById(String id, String objString) {
		mongoTemplate.remove(Query.query(Criteria.where("_id").is(id)),
				objString);
	}

	
	public Object getOne(String id, Class class1) {
		return mongoTemplate.findById(id, class1);
	}

	
	public PagesInfo pagesInfo(PagesInfo pagesInfo, Query query, Class class1) {
		PagesInfo pagesInfo2 = new PagesInfo();
		pagesInfo2.setTotalCount((int) mongoTemplate.count(query, class1));
		pagesInfo2.setPageNum(pagesInfo.getPageNum());
		pagesInfo2.setNumPerPage(pagesInfo.getNumPerPage());
		query.skip((pagesInfo.getPageNum() - 1) * pagesInfo.getNumPerPage());
		query.limit(pagesInfo.getNumPerPage());
		pagesInfo2.setList(mongoTemplate.find(query, class1));
		return pagesInfo2;
	}

	
	public void removeQuery(Query query, Class class1) {
		mongoTemplate.remove(query, class1);
	}

	
	public List getListByQuery(Query query, Class class1) {
		return mongoTemplate.find(query, class1);
	}

	
	public void updateObjcet(Query query, Update update, Class class1) {
		mongoTemplate.updateFirst(query, update, class1);
	}

	
	public void saveAll(List list) {
		mongoTemplate.insertAll(list);
	}

	
	public void updateAll(Query query, Update update, Class class1) {
		mongoTemplate.updateMulti(query, update, class1);
	}
	
	public List findAll(Class class1) {
		return mongoTemplate.findAll(class1);
	}

	public Object findByNoSql(String noSql) {
		return mongoTemplate.executeCommand(noSql);
	}

	public void update(Query query, Class class1, Update update) {
		mongoTemplate.findAndModify(query, update, class1);
	}
}
